﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoCrid.AGNISTableAdapters;
using System.Data.SqlClient;
namespace AutoCrid
{
    public partial class Form1 : Form
    {
        
        //public declaration
        AutoCrid.AGNISTableAdapters.GENERATED_FORMS_LISTTableAdapter Ga = new GENERATED_FORMS_LISTTableAdapter();
        AutoCrid.AGNISTableAdapters.QUESTIONTableAdapter Ta = new QUESTIONTableAdapter();//project.tablesadapters.specific table adapter            
        AutoCrid.PromiseTableAdapters.NewCridsTableAdapter Pa = new AutoCrid.PromiseTableAdapters.NewCridsTableAdapter();
        static AGNIS DSa = new AGNIS();// create a instance of the class (for dataset)
        int Generated_num = 0;
        int A0_num=0;




        public Form1()
        {
            InitializeComponent();         
            CheckState02();
            textBox1.Text = "Completed! with \r\n" + Generated_num.ToString() + " updated rows in Generated_form_list & \r\n" + A0_num.ToString()+" updated rows in A0";
            System.Threading.Thread.Sleep(2000);
            Close();
            Environment.Exit(0);
        }








       
        public void CheckState02()
        {
            //#####################
           Ta.Fill_CRID (DSa.QUESTION); //DataRowView depends on this.            
            BindingSource BSz8 = new BindingSource();
            BSz8.DataSource = DSa.QUESTION;
           // DataRowView dRView0 = (DataRowView)BSz8[0];// populate the row from dataTbl(dataview list?) containing Rich in column name          
           
            //####################
            
            string ID, IDAA, CRID;
            CRID = "";
            ID = "";
            IDAA = "";
            for (int i = 0; i < BSz8.Count; i++)
            {
                DataRowView dRView = (DataRowView)BSz8[i];// populate the row from dataTbl(dataview list?) containing Rich in column name
                
                    
                    if (dRView[1].ToString() == "3126035")
                    {
                        ID = dRView[2].ToString();
                        ID = "8001";//temporary
                      
                    }
                    if (dRView[1].ToString() == "3115881")
                    {
                        IDAA = dRView[2].ToString();
                       
                    }
                    if (dRView[1].ToString() == "2527897")
                    {
                        CRID = dRView[2].ToString();                        
                       
  

                        string C = AutoCrid.Properties.Settings.Default.AGNIS_FORMSConnectionString;
                        using (SqlConnection connection = new SqlConnection(C))
                        {
                            connection.Open();
                            try
                            {
                                using (SqlCommand command = new SqlCommand("update GENERATED_FORMS_LIST set CRID=" + CRID + " where ID=" + ID + " and IDAA=" + IDAA, connection))
                                {
                                    command.ExecuteNonQuery();
                                    Generated_num++;
                                }
                            }
                            catch
                            {
                                connection.Close();
                            }
                        }

                        string C2 = AutoCrid.Properties.Settings.Default.LivePromise;
                        using (SqlConnection connection = new SqlConnection(C2))
                        {
                            connection.Open();
                            try
                            {
                              
                                using (SqlCommand command = new SqlCommand("update A0 set CRID=" + CRID + " where ID=" + ID + " and IDAA=" + IDAA, connection))
                                {
                                    command.ExecuteNonQuery();
                                    A0_num++;
                                }
                            }
                            catch
                            {
                                connection.Close();
                            }
                        }


                    }
                
                
            }
        }
    }
}


